package com.music.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.DialogInterface;
import android.os.Bundle;

import com.music.utilbean.SearchResult;

public class DownLoadMusicFragment extends DialogFragment {
	private SearchResult      searchResult;
	private Tab3ActivityMusic tab3Activity;

	public static DownLoadMusicFragment newInstance(SearchResult searchResult) {
		DownLoadMusicFragment downLoadMusicFragment = new DownLoadMusicFragment();
		downLoadMusicFragment.searchResult = searchResult;
		return downLoadMusicFragment;
	}

	private String[] items;

	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
		tab3Activity = (Tab3ActivityMusic) getActivity();
		items = new String[]{"下载", "取消"};
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		final AlertDialog.Builder builder = new AlertDialog.Builder(tab3Activity);
		builder.setCancelable(true);
		builder.setItems(items, new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				switch (which) {
					case 0:
						//downloadMusic();
						break;
					case 1:
						dialog.dismiss();
						break;
				}

			}
		});
		return builder.show();

	}

	//	protected void downloadMusic() {
	//		Toast.makeText(tab3Activity, "正在下载：" + searchResult.getMusicname(), Toast.LENGTH_SHORT).show();
	//		DownLoadUtil.getInstance().setListener(new DownLoadUtil.OnDownloadListener() {
	//
	//			@Override
	//			public void onFailed(String error) {
	//				Toast.makeText(tab3Activity, error, Toast.LENGTH_SHORT).show();
	//			}
	//
	//			@Override
	//			public void onDownload(String mp3url) {
	//				Toast.makeText(tab3Activity, "数据下载成功", Toast.LENGTH_SHORT).show();
	//				//Uri contentuUri=Uri.fromFile(new File(mp3url));
	//				//Intent mediaScanIntent=new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,contentuUri);
	//
	//			}
	//		}).download(searchResult);
	//	}

	public void show(FragmentManager fragmentManager, String string) {
		FragmentTransaction fTransaction = fragmentManager.beginTransaction();
		fTransaction.add(newInstance(searchResult), string);
		fTransaction.commit();
	}
}
